from PIL import Image


def mean_filter_rgb(image_path, output_path, kernel_size):
    """
    对输入的RGB图像进行均值滤波处理。

    参数:
    image_path (str): 输入图像的文件路径。
    output_path (str): 输出滤波后图像的文件路径。
    kernel_size (int): 滤波核的大小，通常为奇数，如3、5等，表示滤波核是kernel_size * kernel_size的正方形。
    """
    # 打开原始图像
    image = Image.open(image_path)
    # 获取图像的宽度和高度
    width, height = image.size
    # 计算滤波核半径（用于后续遍历像素点周边范围）
    radius = kernel_size // 2
    # 创建新的空白图像，尺寸与原图像相同，模式也相同
    new_image = Image.new(image.mode, (width, height))
    # 遍历图像的每个像素点（避开边缘部分）
    for y in range(radius, height - radius):
        for x in range(radius, width - radius):
            # 分别用于存储红、绿、蓝通道滤波核覆盖像素点的像素值总和
            sum_red = 0
            sum_green = 0
            sum_blue = 0
            # 遍历滤波核范围内的像素点
            for ky in range(y - radius, radius + y):
                for kx in range(x - radius, radius + x):
                    # 获取原图像中对应坐标的像素
                    pixel = image.getpixel((kx, ky))
                    red, green, blue = pixel
                    sum_red += red
                    sum_green += green
                    sum_blue += blue
            # 计算各通道的均值（滤波核范围内像素点个数为kernel_size * kernel_size）
            mean_red = sum_red // (kernel_size * kernel_size)
            mean_green = sum_green // (kernel_size * kernel_size)
            mean_blue = sum_blue // (kernel_size * kernel_size)
            # 将均值设置到新图像对应的坐标位置
            new_image.putpixel((x, y), (mean_red, mean_green, mean_blue))
    # 保存滤波后的图像
    new_image.save(output_path)
    return new_image

image_path = "D:/cangku/computer-image-project-design/test.jpg"
output_path = "D:/cangku/computer-image-project-design/test1.jpg"
kernel_size = 3
mean_filter_rgb(image_path, output_path, kernel_size)